(function(e){var t={};e.publish=function(n,r){t[n]&&e.each(t[n],function(){this.apply(e,r||[])})};e.subscribe=function(e,n){if(!t[e]){t[e]=[]}t[e].push(n);return[e,n]};e.unsubscribe=function(n){var r=n[0];t[r]&&e.each(t[r],function(e){if(this==n[1]){t[r].splice(e,1)}})}})(jQuery)

var VHA_MOD_FILTERED = (function(api) {

	var config, pvt ={};
	
	var all_filters={};

	api.init = function (settings) {
		config = {
			classes: {
				filterContainer: 	"div.vca-mod-dtr"
			}
		};
		pvt.setup();
		
		for ( var i = 0; i < $("div.vha-mod.vca-mod-dtr.clearfix").length; i++) {
			var ele = $("div.vha-mod.vca-mod-dtr.clearfix")[i];
			// remove all extra spaces
			$(ele).parents('.vha-col-cont:first').css("margin-bottom", 0 + "px");
						
						
		}
	};

	pvt.setup = function() {

		pvt.swipe();
		pvt.listeners();
		api.start();
	};

	

	pvt.swipe = function(){
		// Any swipe functionality goes here
	};

	pvt.listeners = function(){
		$.subscribe("/tile/rejig", function($container){ api.rejig($container); });
	};

	// Start :hides all the filter modules and builds the filter options array
	api.start = function(){
		var filtered_modules= $(config.classes.filterContainer);
		$(filtered_modules).hide();
		all_filters=pvt.getallfilters(filtered_modules);
	};

	
	//when the container arrives, unpack and pick the selected options. match and render ! 
	api.rejig = function($container){
		var selected_options = pvt.findselectedOptions($container);
		pvt.matchandRender(all_filters, selected_options);
	
	};
	
	/**
	 * VC-105 starts
	 * automaticall remove all extra spaces when options are selected
	 * 
	 */
	function removeExtraSpaces(to_show) {
		for ( var i = 0; i < $("div.vha-mod.vca-mod-dtr.clearfix").length; i++) {
			var ele = $("div.vha-mod.vca-mod-dtr.clearfix")[i];
			// remove all extra spaces
			$(ele).parents('.vha-col-cont:first').css("margin-bottom", 0 + "px");
		}
		if (to_show) {
			to_show.parents('.vha-col-cont:first').css("margin-bottom", 10 + "px");
		}
	}
	//VC-105 ends
	
	
	
	pvt.matchandRender=function(all_filters, selectedoptions){
		var filtered_modules= $(config.classes.filterContainer);
		$(filtered_modules).hide();

		if(selectedoptions.length>0){
		$(selectedoptions).each(function(i, option) {
				var to_show = pvt.findmatchingfilter(option);
				//VC-105 starts			
				removeExtraSpaces(to_show);
				//VC-105 ends
				if (to_show) {
					$(to_show).slideDown(200, function() {
						var scrollTop = $(this).offset().top;
						$('body').animate({
							scrollTop : scrollTop
						}, 400);
					});
				}
				//return false;//break the loop
			});
		}
	}
	
	pvt.findmatchingfilter=function(option){
		for (var id in all_filters)
			{
			if($.inArray(option.toString(),all_filters[id])>=0){
				return $("#"+id).find(config.classes.filterContainer);
			}
			}

	}

	pvt.findselectedOptions = function($container){
		var list=[];
		$questions =$(container).find("div.on");
		$questions.each(function(){
		$selectedoptions=$(this).find("ul li.on");
			if($selectedoptions.size()>0)
			list.push($($selectedoptions[0]).attr("id").toString());
			
		});
		return list;
	}
	
	
	
	// Build Filter Cache: Store an object of all the filter options 
	pvt.getallfilters = function(filtered_modules) {
		$(filtered_modules).each(
				function() {
					var filters = [];
					$(this).find("div.filter-options .filter-option").each(
							function(filter) {
								filters.push($(this).data("filter-id")
										.toString());
							});
					all_filters[$(this).parent().attr("id")] = filters;
				});
		return all_filters;
	}

	return api;

})(VHA_MOD_FILTERED || {});

$(function() {
	VHA_MOD_FILTERED.init();
});
